package com.dg.lecheng.api.user.service;

import com.dg.lecheng.api.user.model.InfoAuth;
import com.dg.lecheng.api.user.repo.InfoAuthDao;
import com.dg.lecheng.base.core.service.CrudService;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class InfoAuthService extends CrudService<InfoAuthDao, InfoAuth, Integer> {
    @Autowired
    private InfoUserAuthService infoUserAuthService;
    @Autowired
    private InfoRoleAuthService infoRoleAuthService;

    public List<InfoAuth> getListByNameAndCode(String name, String code, PageBounds pageBounds) {
        return curdRepo.getListByNameAndCode(name, code, pageBounds);
    }

    public int getCountByNameAndCode(String name, String code) {
        return curdRepo.getCountByNameAndCode(name, code);
    }


    public List<InfoAuth> findByName(String name, PageBounds pageBounds){
        return curdRepo.findByName(name,pageBounds);
    }

    public List<Map<String,Object>> getListForZtree(){
        return curdRepo.getListForZtree();
    }

    public List<Map<String,Object>> findRoleAuthByRoleIdAndUserId(int userId,int roleId){
        return curdRepo.findRoleAuthByRoleIdAndUserId(userId,roleId);
    }

    public List<Map<String,Object>> findRoleAuthByRoleId(int roleId){
        return curdRepo.findRoleAuthByRoleId(roleId);
    }

    /**
     * 查找 为 type 的父元素
     * @param type
     * @return
     */
    public List<Map<String,Object>> findParentElement(int type){
        return curdRepo.findParentElement(type);
    }

    @Transactional("apiTM")
    public void removeAuthAndRelatedInfo(int authId){
        //1.删除资源
        curdRepo.remove(authId);
        //2.删除role_auth 、 user_auth 关联表中的数据
        infoRoleAuthService.removeByAuthId(authId);
        infoUserAuthService.removeByAuthId(authId);
    }
}
